﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Xml;
using System.Windows.Input;

namespace SHomeWorkshop.LunarMind
{

    public class ImageItem : System.Windows.Controls.TreeViewItem
    {
        public ImageItem(string imageDirectoryFullPath)
        {
            if (Directory.Exists(imageDirectoryFullPath) == false) return;

            string[] imgFileFullPaths = System.IO.Directory.GetFiles(imageDirectoryFullPath);

            #region 树型框项目的头部。
            int index = imageDirectoryFullPath.LastIndexOf('\\');
            string shortname = null;
            if (index >= 0)
            {
                shortname = imageDirectoryFullPath.Substring(index + 1);
            }
            else
            {
                shortname = imageDirectoryFullPath;
            }

            this.Header = shortname;

            //this.Foreground = Brushes.White;
            //this.Background = Brushes.Transparent;

            #endregion

            if (imgFileFullPaths.Length <= 0)
            {
                this.AddChild(new TreeViewItem().Header = "此类别无图像...");
            }
            else
            {
                //将所有项目都添加到树型框的内部。
                WrapPanel wpanel = new WrapPanel();
                wpanel.Width = 240;

                foreach (string filepath in imgFileFullPaths)
                {
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.BeginInit();
                    bitmapImage.UriSource = new Uri(filepath, UriKind.Absolute);
                    bitmapImage.EndInit();

                    CImgButton imgButton = new CImgButton(filepath);
                    imgButton.MainImage.Source = bitmapImage;
                    imgButton.MainImage.Width = bitmapImage.PixelWidth;
                    imgButton.MainImage.Height = bitmapImage.PixelHeight;

                    imgButton.Click += new RoutedEventHandler(imgButton_Click);
                    wpanel.Children.Add(imgButton);
                }

                TreeViewItem tvi = new TreeViewItem();
                tvi.Header = wpanel;

                this.AddChild(tvi);
            }
        }

        void imgButton_Click(object sender, RoutedEventArgs e)
        {
            if (Globals.SelectedTitlesList.Count <= 0 &&
                Globals.MainWindow.SelectedTitleImage == null &&
                Globals.MainWindow.SelectedPictureBox == null)
            {
                MessageBox.Show("请先选中一个标题作为插入图片时的基准位置！或选中一个图片框作为替换位置！",
                    Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            CImgButton button = sender as CImgButton;

            if (button == null) return;

            System.IO.FileInfo fileInfo = new FileInfo(button.ImgFileFullPath);

            if (fileInfo.Exists == false)
            {
                System.Windows.MessageBox.Show("文件不存在！在本程序已开始执行之后，您可能删除了如下位置的文件：\r\n" +
                    button.ImgFileFullPath, Globals.AppName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (Globals.MainWindow.SelectedTitleImage != null || Globals.SelectedTitlesList.Count >= 1)
            {
                Commands.InsertTitleImageCommand.InsertTitleImage(button.ImgFileFullPath);
            }
            else if (Globals.MainWindow.SelectedPictureBox != null)
            {
                ModifingItem mi = new ModifingItem()
                {
                    IsNeedRefreshTitleAndLinesLocation = true,
                    ModifingName = "替换图片框中的图像"
                };

                Globals.MainWindow.SelectedPictureBox.LoadImageFromFile(mi, button.ImgFileFullPath);

                //Globals.MainWindow.Do(mi);//不能重复添加。
            }
        }
    }

    public class CImgButton : Button
    {
        public string ImgFileFullPath { get; set; }

        public Image MainImage { get; set; }

        public CImgButton(string imgFileFullPath)
        {
            Focusable = false;
            Background = Brushes.White;
            ImgFileFullPath = imgFileFullPath;
            MainImage = new Image();
            Content = MainImage;
            TextBlock tb = new TextBlock();

            int index = imgFileFullPath.LastIndexOf('\\');
            string shortname = "";
            if (index >= 0)
            {
                shortname = imgFileFullPath.Substring(index + 1);
            }

            if (shortname != null)
            {
                tb.Text = shortname + "\r\n═════════\r\n点击图片插入到标题";
            }
            else
            {
                tb.Text = "点击图片插入到标题";
            }

            ToolTip = tb;
        }
    }
}
